package com.tianci.de;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.skyworth.deservice.DEDefine;
import com.skyworth.framework.skysdk.ipc.SkyApplication;
import com.skyworth.framework.skysdk.ipc.SkyCmdProcessInstance;
import com.skyworth.framework.skysdk.ipc.SkyCmdProcessInstance.SkyCmdProcessInstanceListener;
import com.skyworth.framework.skysdk.ipc.SkyCmdURI;
import com.skyworth.framework.skysdk.util.SkyObjectByteSerialzie;
import com.tianci.de.api.DEChannelSession;
import com.tianci.de.api.JsonParams;
import com.tianci.de.api.SkyDEApi;
import com.tianci.de.define.DEPlayerDef;
import com.tianci.de.jni.DEJniUtil;

public class DEService extends Service implements SkyCmdProcessInstanceListener {
    private static DEService instance = null;

    public static DEService getDEService() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        DEJniUtil.setContext(this);
        SkyCmdProcessInstance.getCmdProcessInstance(this, this, getPackageName(), "com.tianci.de");
    }

    @Override
    public byte[] onHandler(String fromtarget, String cmd, byte[] body) {
        if (DEDefine.CmdEnum.CMD_REGISTER_DECHANNEL_SESSION.toString().equals(cmd)) {
            if (body != null) {
                DEChannelSession session = DEChannelSession.getDEChannlSession(body);
                Log.e("UIO", fromtarget + " fromtarget  session:" + session.getJsonParam().getSession());
                DEJniUtil.registerSession(session, fromtarget);

                return SkyObjectByteSerialzie.toBytes(true);
            }

            Log.e(SkyDEApi.TAG, fromtarget + " CMD_REGISTER_DECHANNEL_SESSION, body is null");
            return SkyObjectByteSerialzie.toBytes(false);
        } else if (DEDefine.CmdEnum.CMD_UNREGISTER_DECHANNEL_SESSION.toString().equals(cmd)) {
            if (body != null) {
                DEChannelSession session = DEChannelSession.getDEChannlSession(body);
                DEJniUtil.unregisterSession(session);
                return SkyObjectByteSerialzie.toBytes(true);
            }
            return SkyObjectByteSerialzie.toBytes(false);
        } else if (DEDefine.CmdEnum.CMD_READ_DECHANNEL.toString().equals(cmd)) {
            if (body != null) {
                DEChannelSession session = DEChannelSession.getDEChannlSession(body);
                if (!DEJniUtil.containsSession(session)) {
                    Log.e(SkyDEApi.TAG, fromtarget + " read  dechannel but has not registed");
                    return null;
                } else if (!session.hasJsonBody()) {

                    Log.e(SkyDEApi.TAG, fromtarget + " read  dechannel but has noting cmd");
                    return null;
                } else {
                    String returnvalue = DEJniUtil.readDEChannel(session.toSerialize());
                    if (returnvalue != null) {
                        return returnvalue.getBytes();
                    }
                    return null;
                }
            }

        } else if (DEDefine.CmdEnum.CMD_WRITE_DECHANNEL.toString().equals(cmd)) {
            if (body != null) {
                DEChannelSession session = DEChannelSession.getDEChannlSession(body);
                if (!DEJniUtil.containsSession(session)) {
                    Log.e(SkyDEApi.TAG, fromtarget + " write  dechannel but has not registed");
                    return null;
                } else if (!session.hasJsonBody()) {
                    Log.e(SkyDEApi.TAG, fromtarget + " write  dechannel but has noting cmd");
                    return null;
                } else {
                    // JsonParams params = session.getJsonParam();
                    // if (params != null && DEPlayerDef.SESSION.equals(params.getSession()))
                    String ip = session.getAuthIP();
                    String host = session.getHostName();
                    if (DEPlayerDef.LOCAL_IP.equals(ip) && DEPlayerDef.DIAN_SHI_PAI.equals(host)) {
                        Log.i(SkyDEApi.TAG, "WRITE_DECHANNEL, send to dianshipai");
                        JsonParams params = session.getJsonParam();
                        Log.i(SkyDEApi.TAG, "params = " + params.toString());
                        DEJniUtil.notifyInfo(params.getCmd(), params.getParam());
                    } else {
                        DEJniUtil.writeDEChannel(session.toSerialize());
                    }
                    return null;
                }
            }
        }
        return null;
    }

    @Override
    public void onResult(String fromtarget, String cmd, byte[] body) {
    }

    @Override
    public byte[] requestPause(String fromtarget, String cmd, byte[] body) {
        return null;
    }

    @Override
    public byte[] requestResume(String fromtarget, String cmd, byte[] body) {
        return null;
    }

    @Override
    public byte[] requestRelease(String fromtarget, String cmd, byte[] body) {
        return null;
    }

    @Override
    public byte[] requestStartToVisible(String fromtarget, String cmd, byte[] body) {
        return null;
    }

    @Override
    public byte[] requestStartToForground(String fromtarget, String cmd, byte[] body) {
        return null;
    }

    @Override
    public void onCmdConnectorInit() {
        Log.d(SkyDEApi.TAG, "DEService, onCmdConnectorInit()");
        DEJniUtil.init();
        if (instance == null) {
            instance = this;
        }
        SkyCmdURI debroadcast = SkyCmdURI
                .generalBroadcastCmdUri(DEDefine.CmdEnum.CMD_DESERVICE_BOOTCOMPLATE.toString());
        SkyApplication.getApplication().broadcastCmd(this, debroadcast, null);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public String getCmdClassName() {
        return getClass().getName();
    }

}
